﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    class CityInsertViewModel : NotificationObject
    {
        CityInsert gWindow;
        CityViewModel gParentViewModel;

        public CityInsertViewModel(CityInsert pWindow, CityViewModel pParentViewModel)
        {
            gWindow = pWindow;
            gParentViewModel = pParentViewModel;

            List<WProvince> list = WProvince.GetAll();
            foreach(var item in list)
            {
                ProvinceNames.Add(item.ProvinceName.Trim());
            }
        }

        private String cityName;
        public String CityName
        {
            get
            {
                return cityName;
            }
            set
            {
                cityName = value;
                RaisePropertyChanged("CityName");
            }
        }

        private int personnelUID;
        public int PersonnelUID
        {
            get
            {
                return personnelUID;
            }
            set
            {
                personnelUID = value;
                RaisePropertyChanged("PersonnelUID");
            }
        }

        private List<String> provinceNames = new List<String>();
        public List<String> ProvinceNames
        {
            get
            {
                return provinceNames;
            }

            set
            {
                provinceNames = value;
                RaisePropertyChanged("ProvinceNames");
            }
        }

        private String provinceSelected;
        public String ProvinceSelected
        {
            get
            {
                return provinceSelected;
            }

            set
            {
                provinceSelected = value;
                RaisePropertyChanged("ProvinceSelected");
            }
        }

        protected DelegateCommand<object> insertCommand;
        public DelegateCommand<object> InsertCommand
        {
            get
            {
                return insertCommand ?? (insertCommand = new DelegateCommand<object>(AInsertCommand, BInsertCommand));
            }
        }

        protected virtual void AInsertCommand(object obj)
        {
            List<WCity> list = new List<WCity>();
            WCity item = new WCity();
            item.CityName = CityName;
            item.PersonnelUID = PersonnelUID;
            item.ProvinceUID = WProvince.GetProvinceUID(ProvinceSelected);
            list.Add(item);
            HttpResponse response = WCity.Insert(list);

            gWindow.Close();
            gParentViewModel.UpdateList();
        }

        private bool BInsertCommand(object arg)
        {
            return true;
        }

        protected DelegateCommand<object> quitCommand;
        public DelegateCommand<object> QuitCommand
        {
            get
            {
                return quitCommand ?? (quitCommand = new DelegateCommand<object>(AQuitCommand, BQuitCommand));
            }
        }

        protected virtual void AQuitCommand(object obj)
        {
            gWindow.Close();
            gParentViewModel.UpdateList();
        }

        private bool BQuitCommand(object arg)
        {
            return true;
        }
    }
}
